/******************************************************************************** /
 * DictionaryModelReader.java - JPLearning Project								*
 * 																				*
 * 		This program is free software; you can redistribute it and/or modify	*
 *		it under the terms of the GNU General Public License as published by	*
 *		the Free Software Foundation; version 2 of the License.					*
 *																				*
 *		This program is distributed in the hope that it will be useful,			*
 *		but WITHOUT ANY WARRANTY; without even the implied warranty of			*
 *		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			*
 *		GNU General Public License for more details.							*
 *																				*
 ********************************************************************************/
package org.jplearning.io;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import org.jplearning.model.DictionaryModel;
import org.jplearning.model.WordEntry;
import org.jplearning.model.text.HiraganaString;
import org.jplearning.model.text.IText;
import org.jplearning.model.text.KanjiString;
import org.jplearning.model.text.KatakanaString;
import org.jplearning.model.text.RomanjiString;

/**
 * @author A144601
 */
public class DictionaryModelReader {
	public DictionaryModel readFromFile(File file) throws IOException{
		DictionaryModel model = new DictionaryModel();
		
		// Opens the file
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		
		String line;
		
		while(!"".equals(line = readLine(bis))){
			Object[] res = parseLine(line);
			IText text = (IText)res[0];
			WordEntry entry = (WordEntry)res[1];
			model.addEntry(text, entry);
		}
		bis.close();
		fis.close();
		return model;
	}

	private IText getType(String tipo, String texto){
		if("HG".equals(tipo)){
			return new HiraganaString(texto);
		}
		else if("KT".equals(tipo)){
			return new KatakanaString(texto);
		}
		else if("KJ".equals(tipo)){
			return new KanjiString(texto);
		}
		else{ // "?"
			return new RomanjiString(texto);
		}
	}
	
	/**
	 * @param line
	 * @return
	 */
	private Object[] parseLine(String line) {
		// Cogemos lo que existe entre [X]
		int fntipo = line.indexOf("]");
		int prsignf = line.indexOf("/");
		
		String tipo = line.substring(1, fntipo);

		// Cogemos la cadena
		String cadena = line.substring(fntipo+1, prsignf);
		String resto = line.substring(prsignf+1);
		System.out.println(resto);
		
		IText text = getType(tipo, cadena);
		
		// Lo que quedan son los significados
		// Por cada uno procesamos /(tipo)+cadena/
		StringTokenizer tokenizer = new StringTokenizer(resto, "/");
		
		String palabra = tokenizer.nextToken();
		fntipo = palabra.indexOf(")");
		
		WordEntry entry = null;
		if(fntipo > -1){
			String tipoabv = palabra.substring(0, fntipo+1);
			System.out.println(WordEntry.getComplete(tipoabv));

			String significado = palabra.substring(fntipo + 1);
			entry = new WordEntry(WordEntry.getComplete(tipoabv));
			List lista = new LinkedList();
			lista.add(significado);
			entry.setSignificados(lista);
		}
		return new Object[]{text, entry};
	}

	/**
	 * @param bis
	 * @return
	 */
	private String readLine(BufferedInputStream bis) throws IOException{
		StringBuffer buffer = new StringBuffer();
		char read;
		while( (read = (char)bis.read()) != -1 && read != '\n' ){
			buffer.append(read);
		}
		return new String(buffer.toString().getBytes("UTF-8"));
	}
}
